package com.example.demo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 序列化工具类
 *
 * @author wolong
 */
public final class SystemUtil {
    private static final Logger logger = LoggerFactory.getLogger(SystemUtil.class);

    private static final Runtime RUNTIME = Runtime.getRuntime();
    private static long recordTime = 0;

    public static void recordTime() {
        recordTime = System.currentTimeMillis();
    }

    public static long difference() {
        return System.currentTimeMillis() - recordTime;
    }

    public static boolean isTimeout(long expireTime) {
        return System.currentTimeMillis() - recordTime > expireTime;
    }

    /**
     * 修改采样率
     *
     * @param filePath
     * @param sampleRate
     */
    public static boolean formatSampleRate(String filePath, String targetPath, int sampleRate) {
        int volumeUp = 30;
//        String cmd = "ffmpeg -i " + filePath + " -ar " + sampleRate + "  " + targetPath;
        String cmd = "echo y |ffmpeg -i " + filePath + " -ar " + sampleRate + "  -af volume=+" + volumeUp + "dB " + targetPath;
        logger.info("cmd=" + cmd);
        return cmd(cmd);
    }

    public static void waitTime(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
                logger.error("异常",e);
}
    }


    public static boolean cmd(String cmd) {
        String strCmd = "cmd /c " + cmd;

        try {
            RUNTIME.exec(strCmd);
            return true;
        } catch (IOException e) {
            logger.info("执行转采样率失败", e);
            return false;
        }
    }
}
